/**
 * Mock数据入口文件
 * 用于导出所有模拟数据和接口
 * @author LYC
 * @date 2025-09-16
 */

import chatData, {
  currentUser,
  generateContacts as generateChatContacts,
  generateChatSessions,
  generateMessageHistory,
  getChatMessages,
  generateChatData
} from './chatData'

import contactData, {
  generateContacts,
  generateContactCategories,
  generateDetailedContacts,
  generateContactData
} from './contactData'

import Mock from 'mockjs'

// 模拟用户登录API响应
Mock.mock('/api/auth/login', 'post', () => {
  return {
    code: 200,
    msg: 'success',
    data: {
      user: currentUser,
      token: 'mock-token-xxxx-yyyy-zzzz'
    }
  }
})

// 模拟获取联系人列表API
Mock.mock('/api/contacts', 'get', () => {
  return {
    code: 200,
    msg: 'success',
    data: {
      contacts: generateContacts(30)
    }
  }
})

// 模拟获取联系人详细信息API
Mock.mock(/\/api\/contacts\/detail\/.*/, 'get', (options: any) => {
  // 从URL中提取联系人ID
  const contactId = options.url.split('/').pop()
  const allContacts = generateDetailedContacts(30)
  const contact = allContacts.find((c) => c.id === contactId) || allContacts[0]

  return {
    code: 200,
    msg: 'success',
    data: {
      contact
    }
  }
})

// 模拟获取联系人分类API
Mock.mock('/api/contacts/categories', 'get', () => {
  const data = generateContactData()
  return {
    code: 200,
    msg: 'success',
    data: {
      categories: data.categories
    }
  }
})

// 模拟获取会话列表API
Mock.mock('/api/chat/sessions', 'get', () => {
  const contacts = generateChatContacts(15)
  return {
    code: 200,
    msg: 'success',
    data: {
      sessions: generateChatSessions(contacts)
    }
  }
})

// 模拟获取特定聊天历史API
Mock.mock(/\/api\/chat\/messages\/.*/, 'get', (options: any) => {
  // 从URL中提取联系人ID
  const contactId = options.url.split('/').pop()

  return {
    code: 200,
    msg: 'success',
    data: {
      messages: getChatMessages(contactId)
    }
  }
})

// 模拟发送消息API
Mock.mock('/api/chat/send', 'post', (options: any) => {
  const body = JSON.parse(options.body)
  return {
    code: 200,
    msg: 'success',
    data: {
      message: {
        id: `new-msg-${Date.now()}`,
        senderId: currentUser.id,
        receiverId: body.receiverId,
        content: body.content,
        type: body.type || 'text',
        timestamp: new Date(),
        status: 'sent'
      }
    }
  }
})

// 提供通过Pinia store使用的测试数据
export const getInitialChatData = () => {
  return generateChatData()
}

export {
  currentUser,
  generateChatContacts,
  generateContacts,
  generateContactCategories,
  generateDetailedContacts,
  generateContactData,
  generateChatSessions,
  generateMessageHistory,
  getChatMessages
}

export default {
  chatData,
  contactData,
  getInitialChatData
}
